package com.vinoxm.rss.dao;

import com.vinoxm.common.core.dao.BaseDao;
import com.vinoxm.rss.model.RssResult;
import com.vinoxm.rss.model.RssSubscribe;
import com.vinoxm.rss.vo.RssResultVo;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public interface RssDao extends BaseDao {

    @Insert("INSERT IGNORE INTO rss_subscribe(name,name_jp, url, regex, season, start_time, cover, fin, is_short, anime_type, goon, staff, cast, origin_type, type_tag, broadcast) " +
            "VALUE(#{name},#{nameJP}, #{url}, #{regex}, #{season}, #{startTime}, #{cover}, #{fin}, #{isShort},#{animeType}, #{goon}, #{staff}, #{cast}, #{originType}, #{typeTag}, #{broadcast})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insertOne(RssSubscribe rssSubscribe);

    @Insert({"<script>" +
            "INSERT IGNORE INTO rss_subscribe" +
            "(name,name_jp, url, regex, season, start_time, cover, fin, is_short, anime_type, goon, staff, cast, origin_type, type_tag, broadcast) " +
            "VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.name},#{item.nameJP}, #{item.url}, #{item.regex}, #{item.season}, #{item.startTime}, #{item.cover}, #{item.fin}, #{item.isShort}, #{item.animeType}, #{item.goon}, #{item.staff}, #{item.cast}, #{item.originType}, #{item.typeTag}, #{item.broadcast})" +
            "</foreach>" +
            "</script>"})
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insertMany(@Param(value = "list") List<RssSubscribe> rssSubscribeList);

    @Insert("INSERT IGNORE INTO rss_result(id, pid, title, torrent, pub_date, tracker, episode) VALUE(#{id}, #{pid}, #{title}, #{torrent}, #{pubDate}, #{tracker}, #{episode})")
//    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insertOneResult(RssResult rss);

    @Insert({"<script>" +
            "INSERT IGNORE INTO rss_result" +
            "(id, pid, title, torrent, pub_date, tracker, episode) " +
            "VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.id}, #{item.pid}, #{item.title}, #{item.torrent}, #{item.pubDate}, #{item.tracker}, #{item.episode})" +
            "</foreach>" +
            "</script>"})
//    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insertManyResult(@Param(value = "list") List<RssResult> rssResultsList);

    @Select("SELECT id,name,name_jp nameJP,regex,url,season,start_time startTime,cover,fin,is_short isShort,anime_type animeType,goon,staff,cast,origin_type originType, type_tag typeTag, broadcast FROM rss_subscribe WHERE season=#{season}")
    @Results(value = {
            @Result(column = "id", property = "id", id = true),
            @Result(column = "id", property = "result", javaType = ArrayList.class, many = @Many(select = "com.vinoxm.rss.dao.RssDao.selectResultByPidWithOutHide")),
            @Result(column = "id", property = "link", javaType = ArrayList.class, many = @Many(select = "com.vinoxm.rss.dao.RssLinkDao.selectByPid")),
            @Result(column = "id", property = "copyright", javaType = ArrayList.class, many = @Many(select = "com.vinoxm.rss.dao.RssCopyrightDao.selectByPid"))
    })
    List<RssSubscribe> selectRssSubscribeBySeasonOld(String season);

    @Select("SELECT rs.id,rs.name,rs.name_jp nameJP,rs.regex,rs.url,rs.season,rs.start_time startTime,rs.cover,rs.fin,rs.is_short isShort,rs.anime_type animeType, MAX(rr.pub_date) lastPub, COUNT(rr.id) count,true lazy,goon,staff,cast,origin_type originType, type_tag typeTag, broadcast " +
            "FROM rss_subscribe rs " +
            "LEFT JOIN  rss_result rr ON rs.id=rr.pid AND rr.hide=0 " +
            "WHERE rs.season=#{season} " +
            "GROUP BY rs.id")
    List<RssSubscribe> selectRssSubscribeBySeason(String season);

    @Select("<script>" +
            "SELECT id,name,name_jp nameJP,regex,url,season,start_time startTime,cover,fin,is_short isShort,anime_type animeType,goon,staff,cast,origin_type originType, type_tag typeTag, broadcast " +
            "FROM rss_subscribe " +
            "WHERE season IN ("  +
            "<foreach collection='list' item='item' separator=','>" +
            "#{item}" +
            "</foreach>" +
            ")" +
            "</script>")
    List<RssSubscribe> selectRssSubscribeBySeasons(List<String> list);

    @Select("SELECT rs.id,rs.name,rs.name_jp nameJP,rs.regex,rs.url,rs.season,rs.start_time startTime,rs.cover,rs.fin,rs.is_short isShort,rs.anime_type animeType, MAX(rr.pub_date) lastPub, COUNT(rr.id) count,true lazy,goon,staff,cast,origin_type originType, type_tag typeTag, broadcast " +
            "FROM rss_subscribe rs " +
            "LEFT JOIN  rss_result rr ON rs.id=rr.pid AND rr.hide=0 " +
            "WHERE rs.fin='N' AND rs.goon=1 AND rs.season<#{season} " +
            "GROUP BY rs.id")
    List<RssSubscribe> selectRssSubscribeCauseGoon(String season);

    @Select("SELECT id,name,name_jp nameJP,regex,url,season,start_time startTime,cover,fin,is_short isShort,anime_type animeType FROM rss_subscribe WHERE season=#{season} and fin='N'")
    List<RssSubscribe> selectRssSubscribeBySeasonWithoutFin(String season);

    @Select("SELECT id,name,regex,url,season" +
//            ",start_time startTime,cover,fin,is_short isShort,anime_type animeType" +
            " FROM rss_subscribe WHERE fin='N' ORDER BY season")
    List<RssSubscribe> selectRssSubscribeWithoutFin();

    @Delete("DELETE rs,rr FROM rss_subscribe rs LEFT JOIN rss_result rr ON rs.id=rr.pid LEFT JOIN rss_link rl ON rs.id=rl.pid LEFT JOIN rss_copyright rc ON rs.id=rc.pid WHERE rs.id=#{id}")
    void deleteOne(RssSubscribe rssSubscribe);

    @Delete("<script>" +
            "DELETE rs,rr FROM rss_subscribe rs LEFT JOIN rss_result rr ON rs.id=rr.pid LEFT JOIN rss_link rl ON rs.id=rl.pid LEFT JOIN rss_copyright rc ON rs.id=rc.pid WHERE rs.id IN (" +
            "<foreach collection='list' item='item' separator=','>" +
            "#{item.id}" +
            "</foreach>" +
            ")" +
            "</script>")
    void deleteMany(@Param(value = "list") List<RssSubscribe> rssSubscribe);

    @Delete("DELETE FROM rss_result WHERE id=#{id}")
    int deleteOneRssResult(RssResult result);

    @Update("UPDATE rss_result SET hide=#{hide} WHERE id=#{id}")
    int hideOneRssResult(RssResult result);

    @Select("<script>" +
            "SELECT id,name,name_jp nameJP,url,regex,season,start_time startTime,cover,fin,is_short isShort,anime_type animeType FROM rss_subscribe" +
            "<where>" +
            "<if test='season!=null'>" +
            "season=#{season} " +
            "</if>" +
            "<if test='name!=null'>" +
            "AND name LIKE CONCAT('%',#{name},'%') " +
            "</if>" +
            "</where>" +
            "</script>")
    @Results(value = {
            @Result(column = "id", property = "id", id = true),
            @Result(column = "id", property = "result", javaType = ArrayList.class, many = @Many(select = "com.vinoxm.rss.dao.RssDao.selectResultByPidWithOutHide"))
    })
    List<RssSubscribe> selectAllOld(HashMap<String, Object> params);

    @Select("<script>" +
            "SELECT rs.id,rs.name,rs.name_jp nameJP,rs.regex,rs.url,rs.season,rs.start_time startTime,rs.cover,rs.fin,rs.is_short isShort,rs.anime_type animeType, MAX(rr.pub_date) lastPub, COUNT(rr.id) count,true lazy,goon,staff,cast,origin_type originType, type_tag typeTag, broadcast " +
            "FROM rss_subscribe rs " +
            "LEFT JOIN rss_result rr ON rs.id=rr.pid AND rr.hide=0 " +
            "<where>" +
            "<if test='season!=null'>" +
            "rs.season=#{season} " +
            "</if>" +
            "<if test='name!=null'>" +
            "AND rs.name LIKE CONCAT('%',#{name},'%') " +
            "</if>" +
            "</where>" +
            "GROUP BY rs.id" +
            "</script>")
    List<RssSubscribe> selectAll(@Param("name") String name, String season);

    @Select("SELECT id,name,name_jp nameJP,url,regex,season,start_time startTime,cover,fin,is_short isShort,anime_type animeType FROM rss_subscribe WHERE id=#{id}")
    RssSubscribe selectOneById(long id);

    @Select("<script>" +
            "SELECT id,name,url,regex,season,start_time startTime,cover,fin " +
            "FROM rss_subscribe " +
            "WHERE id in (" +
            "<foreach collection='ids' item='item' separator=','>" +
            "#{item}" +
            "</foreach>" +
            ")" +
            "</script>")
    List<RssSubscribe> selectManyByIds(List<Long> ids);

    @Select("SELECT id,pid,title,torrent,pub_date pubDate,hide,tracker,episode FROM rss_result WHERE pid=#{pid} ORDER BY -episode")
    List<RssResult> selectResultByPid(long pid);

    @Select("SELECT id,pid,title,torrent,pub_date pubDate,hide,tracker,episode FROM rss_result WHERE pid=#{pid} AND hide=0 ORDER BY -episode")
    List<RssResult> selectResultByPidWithOutHide(long pid);

    @Update("UPDATE rss_subscribe SET name=#{name},name_jp=#{nameJP},url=#{url},regex=#{regex},season=#{season},cover=#{cover},start_time=#{startTime},fin=#{fin},is_short=#{isShort},anime_type=#{animeType},goon=#{goon},staff=#{staff},cast=#{cast},origin_type=#{originType},broadcast=#{broadcast},type_tag=#{typeTag} WHERE id=#{id}")
    void updateOneById(RssSubscribe rssSubscribe);

    @Update("UPDATE rss_subscribe SET name=#{name},name_jp=#{nameJP},start_time=#{startTime},is_short=#{isShort},anime_type=#{animeType},staff=#{staff},cast=#{cast},origin_type=#{originType},tag_type=#{tagType},broadcast=#{broadcast} WHERE id=#{id}")
    void updateOneDetailById(RssSubscribe rssSubscribe);

    @Update("<script>" +
            "UPDATE rss_subscribe SET " +
            "<foreach collection='map.entrySet()' item='value' index='key' separator=','>" +
            "${key}=#{value}" +
            "</foreach>" +
            " WHERE id=#{id}" +
            "</script>")
    void updateOneColumnsById(@Param("id") Long id, @Param("map")HashMap<String, Object> params);

    @Update("UPDATE rss_result SET title=#{title},torrent=#{torrent},pub_date=#{pubDate},tracker=#{tracker},episode=#{episode} WHERE id=#{id}")
    int updateOneResultById(RssResultVo rssResult);

    @Delete("DELETE FROM rss_result WHERE pid=#{pid}")
    void deleteManyRssResultByPid(long pid);

    @Select("SELECT season FROM rss_subscribe GROUP BY season")
    List<String> selectRssSubscribeSeason();

    @Select("<script>" +
            "SELECT season FROM rss_subscribe WHERE id IN (" +
            "<foreach collection='ids' item='item' separator=','>" +
            "#{item.id}" +
            "</foreach>" +
            ") GROUP BY season" +
            "</script>")
    List<String> selectRssSubscribeSeasonByIds(List<RssSubscribe> ids);

    @Update("<script>" +
            "UPDATE rss_subscribe SET fin=#{fin} " +
            "<if test='fin==\"Y\"'>" +
            ",goon=0" +
            "</if>" +
            "WHERE id=#{id}" +
            "</script>")
    int updateRssSubscribeFin(RssSubscribe rss);

    @Select("<script>" +
            "SELECT rs.season from rss_subscribe rs INNER JOIN rss_result rr on rr.pid=rs.id AND rr.hide=0 WHERE rr.id IN (" +
            "<foreach collection='ids' item='item' separator=','>" +
            "#{item}" +
            "</foreach>" +
            ") GROUP BY rs.season" +
            "</script>")
    List<String> selectSeasonByResultIds(List<Long> ids);

    @Select("SELECT  COALESCE(MAX(id),0) id FROM rss_result")
    Long selMaxResultId();

    @Select("SELECT season from rss_subscribe where id=#{id}")
    String selectRssSubscribeSeasonById(long id);

    @Insert("INSERT INTO test(user) VALUE('zhangsan')")
    void insertTest();
}
